package com.mgg.user.consumer;

import com.mgg.common.consumer.MessageConsumer;
import com.mgg.common.messaging.RegistShopkeeperProcessor;
import com.mgg.common.payload.RegistShopkeeperPayload;
import com.mgg.common.util.JsonUtils;
import com.mgg.user.service.user.UserActivityService;
import com.mgg.user.service.user.UserSalaryService;
import com.mgg.user.service.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

@Component
public class RegistShopkeeperMessageConsumer implements MessageConsumer<RegistShopkeeperPayload> {

	Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private UserSalaryService userSalaryService;
	
	@Autowired
	private UserActivityService userActivityService;
	
    @StreamListener(RegistShopkeeperProcessor.INPUT)
	@Override
	public void receiveMessage(RegistShopkeeperPayload payload) {
    	log.info("MQ【注册店主】 payloadw = {}", JsonUtils.toJson(payload));
    	String userId = payload.getUserId();
    	String shopCode = payload.getShopCode();
    	String registTime = payload.getRegistTime();
    	Integer orderType = payload.getOrderType();
    	Integer giveNum = payload.getGiveNum();
    	try {
			userService.vipToShopKeeper(userId, shopCode, registTime, "购买大礼包成为店主");
			userSalaryService.payExpandingSalary(userId, orderType, registTime);
			userActivityService.sendActCode(userId, giveNum);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			log.error("VIP：{} 升级为 店主失败，shopCode = {}", userId, shopCode);
		}
	}
}
